﻿using MiniFox.Caching;
using MiniFox.Components;
using MiniFox.Net;
using MiniFox.Net.Http;
using MiniFox.Platform.Applications.Auth;
using MiniFox.Platform.Applications.Configurations;
using MiniFox.Platform.Applications.Models;
using MiniFox.Platform.Applications.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Platform.Applications
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class ApiServiceBase : Component
    {

        SystemPrincipal _principal;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="principal"></param>
        protected ApiServiceBase(SystemPrincipal principal)
        {
            _principal = principal;
        }
        /// <summary>
        /// 
        /// </summary>
        protected SystemPrincipal Principal
        {
            get
            {
                return this._principal;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected SystemIdentity Identity
        {
            get
            {
                return this.Principal.Identity as SystemIdentity;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [AutoConfigure]
        protected virtual DesktopConfiguration DesktopConfiguration
        {
            get;
            set;
        }
        CacheProvider cacheProvider;
        /// <summary>
        /// 
        /// </summary>
        protected CacheProvider Cache
        {
            get
            {
                if (cacheProvider == null)
                {
                    cacheProvider = CacheService.GetCacheProvider(this.DesktopConfiguration.CacheName);
                }
                return cacheProvider;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="adapterName"></param>
        /// <param name="apiName"></param>
        /// <returns></returns>
        protected virtual HttpService GetHttpServiceByName(string adapterName, string apiName)
        {
            var container = WebAdapterFactory.Container;
            var httpAdapter = container.GetHttpAdapterByName(adapterName);
            var httpService = httpAdapter.GetService(apiName);
            return httpService;
        }

    }
}
